package com.healthlink.appcloudcalllib.net;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;

import static java.lang.String.valueOf;

import com.healthlink.appcloudcalllib.common.Constants;
import com.healthlink.appcloudcalllib.utils.CommonUtil;
import com.healthlink.appcloudcalllib.utils.Out;

public class OkUtils {

    public static void GetOk(String url, String accessToken,Callback callback){
        OkHttpClient okHttpClient=new OkHttpClient();
        url= Constants.HOST+url;
//        Out.outHttpLog("请求URL=="+url);
        Out.out("GET请求URL=="+url);
        Out.out("GET请求Authorization=="+accessToken);
        Request request = new Builder()
                .url(url)
                .addHeader("Authorization", accessToken)
                .get()
                .build();
        if (okHttpClient==null){
            okHttpClient=new OkHttpClient();
        }
        Call call = okHttpClient.newCall(request);

        call.enqueue(callback);

    }

    public static Builder NetMode(int type, RequestBody body) {
        if (type == 1) {
            return new Builder().method("DELETE", body);
        } else if (type == 2) {
            return new Builder().method("POST", body);
        } else if (type == 3) {
            return new Builder().method("GET", null);
        } else if (type == 4) {
            return new Builder().method("PUT", body);
        } else if (type == 5) {
            return new Builder().method("PATCH", body);
        } else {
            return new Builder().method("POST", body);
        }
    }
    /**
     * 执行普通的post请求，参数集合全部转为json
     *
     * @param
     * @param callback 回调接口
     */
    public static  void performByJson(final Context context, String mUrl, HashMap<Object, Object> hashMap, int perType, final Callback callback) {

        mUrl= Constants.HOST+mUrl;
        OkHttpClient mClient=new OkHttpClient();
        String json = JsonUtil.getJsonStringByEntity(hashMap);

        RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);

        String utoken=CommonUtil.token;
        if (TextUtils.isEmpty(utoken)){
            utoken="1";
        }


        final Request request = NetMode(perType, body)
                .url(mUrl)
                .addHeader("Cookie", "JSESSIONID= " + utoken)
                .build();
        if (mClient==null){
            mClient=new OkHttpClient();
        }
        Call call = mClient.newCall(request);

        call.enqueue(callback);

    }
    /**
     * 执行普通的post请求，参数集合全部转为json
     *
     * @param
     * @param callback 回调接口
     */
    public static  void performByJsonArray(final Context context, String mUrl,String accessToken, LinkedHashMap<Object, Object> hashMap, int perType, final Callback callback) {

        String json = JsonUtil.getJsonStringByEntity(hashMap);
        OkHttpClient mClient=new OkHttpClient();

        RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);
         mUrl=Constants.HOST+mUrl;
         Out.outHttpLog("url=="+mUrl);
         Out.outHttpLog("param=="+json);

         Out.out("Post请求："+mUrl);
        Out.out("param=="+json);
        Out.out("Authorization=="+accessToken);
         Request request = null;
         if (TextUtils.isEmpty(accessToken)){
             request= NetMode(perType, body)
                     .url(mUrl)
                     .build();
         }else{
             request= NetMode(perType, body)
                     .url(mUrl)
                     .addHeader("Authorization", accessToken)
                     .build();
         }

        if (mClient==null){
            mClient=new OkHttpClient();
        }
        Call call = mClient.newCall(request);

        call.enqueue(callback);
    }



}
